<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="250px" size="medium">
      <el-form-item label="通知渠道" prop="noticChannelIds">
        <el-transfer
          v-loading="loading"
          :titles="titles"
          filterable
          :filter-method="filterMethod"
          filter-placeholder="请选择渠道"
          :props="{key: 'channelId',label: 'channelName'}"
          v-model="form.noticChannelIds"
          :data="channelList"
          @change="changeClick"
        >
        </el-transfer>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { editSysConfig, getSysConfigById } from '@/api/admin/config'
  import { listAllChannel } from '@/api/admin/channel'

  export default {
  name: "AdminConfigSMSChannel",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 配置对象
      config: {},
      titles: ['候选渠道','已选渠道'],
      //渠道列表
      channelList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {noticChannelIds:[]},
      // 表单校验
      rules: {
        noticChannelIds: [
          { required: true, message: "渠道至少要选择一个", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getChannelList();
  },
  methods: {
    getChannelList() {
      this.loading=true;
      listAllChannel().then(response => {
        this.channelList = response.data;
        this.loading=false;
        this.getList();
      });
    },
    filterMethod(query, item) {
      return item.channelName.indexOf(query) > -1;
    },
    /** 获取配置信息 */
    getList() {
      this.loading = true
      getSysConfigById(3).then(response => {
          this.loading = false
          this.config = response.data
          this.form.noticChannelIds = JSON.parse(response.data.configItemValue)
        }
      )
    },
    changeClick(value,b,a) {
      this.loading = true
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.noticChannelIds != undefined && this.form.noticChannelIds.length > 0) {
            this.config.configItemValue = JSON.stringify(this.form.noticChannelIds)
            editSysConfig(this.config).then(response => {
              this.loading = false
              if (response.code === 0) {
                this.msgSuccess('修改成功')
                this.open = false
                // this.getList();
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            this.loading = false
            this.msgError('数据异常,无法保存')
          }
        } else {
          this.loading = false
        }
      })
    }
  }
};
</script>
